﻿ package jakey.SalaryCount;

import jakey.SalaryCount.SQLite.dbHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddTeacherActivity extends Activity {
	private dbHelper db;
	private EditText et_name;
	private EditText et_salary;
	private EditText et_major;
	private EditText et_info;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.addteacher);
        
        et_name = (EditText) findViewById(R.id.et_name);
        et_salary = (EditText) findViewById(R.id.et_salary);
		et_major = (EditText) findViewById(R.id.et_major);
		et_info = (EditText) findViewById(R.id.et_info);
        
        Button AddButton = (Button)findViewById(R.id.addteacher_addbtn);       
        AddButton.setOnClickListener(new AddButtonListener());
        
        Button clearButton = (Button)findViewById(R.id.addteacher_clearbtn);
        clearButton.setOnClickListener(new ClearBtnListener());
	}
	
	class AddButtonListener implements OnClickListener {
		
		public void onClick(View v){
			
			
			String user_name = et_name.getText().toString();
			if(user_name.equals("")) {
				showMsg("请填写姓名");
				//获取焦点
				et_name.requestFocus();
				return;			
			}		
			
			String str_salary = et_salary.getText().toString();
			if(str_salary.equals("")) {
				showMsg("请填写底薪");
				et_salary.requestFocus();
				return;
			}
			int salary = 0;
			try{
				salary = Integer.parseInt(str_salary);
			}catch (Exception e) {
				// TODO: handle exception
				showMsg("底薪必须为数字");
				et_salary.setText("");
				et_salary.requestFocus();
				return;
			}	
			
			String major = et_major.getText().toString();
			if(major.equals("")) {
				showMsg("请填写科目");
				et_major.requestFocus();
				return;
			}
			
			String info = et_info.getText().toString();			
			
			db = new dbHelper(AddTeacherActivity.this, "salaryCount_db", null);
			long res = db.insert(user_name, salary, major, info);
			if(res >= 1 ){
				et_name.setText("");
				et_salary.setText("");
				et_major.setText("");
				et_info.setText("");
				showMsg("操作成功");
			} else {
				showMsg("操作失败，请联系作者J^_^Q");
			}
			db.close();
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(db != null) {
			db.close();
		}
	}

	//完成添加，退出当前Activity
	class ClearBtnListener implements OnClickListener {
		public void onClick(View v){
			et_info.setText("");
			et_major.setText("");
			et_name.setText("");
			et_salary.setText("");
		}
	}
	
	//提示信息
	public void showMsg(String msg) {
		Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
		toast.show();
	}
}
